package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqSolidliquidProduceMaintenanceMonthMapper;
import com.guofen.pojo.SjqSolidliquidProduceElectricity;
import com.guofen.pojo.SjqSolidliquidProduceMaintenanceMonth;
import com.guofen.service.sjq.SjqSolidliquidProduceMaintenanceMonthService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqSolidliquidProduceMaintenanceMonthServiceImpl implements SjqSolidliquidProduceMaintenanceMonthService {

    @Autowired
    private SjqSolidliquidProduceMaintenanceMonthMapper sjqSolidliquidProduceMaintenanceMonthMapper;

    @Override
    public boolean add_recorder(String equipment_name, String operator, String maintenance_content, String remarks) {
        //获取当前日期
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String date = simpleDateFormat.format(now);
        //获取计划保养日期
        String maintenance_time_plan = "2022-01-01";
        //获取设备保养实施日期
        String maintenance_time_implement = DateAndTimeUtil.getDate();
        //设备是否正常
        String is_normal;
        if (remarks == null || remarks.equals("")){
            is_normal = "正常";
        }else {
            is_normal = "异常";
        }
        SjqSolidliquidProduceMaintenanceMonth sjqSolidliquidProduceMaintenanceMonth = new SjqSolidliquidProduceMaintenanceMonth();
        sjqSolidliquidProduceMaintenanceMonth.setDate(date);
        sjqSolidliquidProduceMaintenanceMonth.setMaintenanceTimePlan(maintenance_time_plan);
        sjqSolidliquidProduceMaintenanceMonth.setMaintenanceTimeImplement(maintenance_time_implement);
        sjqSolidliquidProduceMaintenanceMonth.setEquipmentName(equipment_name);
        sjqSolidliquidProduceMaintenanceMonth.setMaintenanceContent(maintenance_content);
        sjqSolidliquidProduceMaintenanceMonth.setIsNormal(is_normal);
        sjqSolidliquidProduceMaintenanceMonth.setRemarks(remarks);
        sjqSolidliquidProduceMaintenanceMonth.setOperator(operator);
        int result = sjqSolidliquidProduceMaintenanceMonthMapper.insert(sjqSolidliquidProduceMaintenanceMonth);
        if (result == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean add_examiner(Integer id, String examiner, String remarks) {
        SjqSolidliquidProduceMaintenanceMonth sjqSolidliquidProduceMaintenanceMonth = sjqSolidliquidProduceMaintenanceMonthMapper.selectByPrimaryKey(id);
        sjqSolidliquidProduceMaintenanceMonth.setExaminer(examiner);
        sjqSolidliquidProduceMaintenanceMonth.setRemarks(remarks);
        int result = sjqSolidliquidProduceMaintenanceMonthMapper.updateByPrimaryKeySelective(sjqSolidliquidProduceMaintenanceMonth);
        if (result == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public SjqSolidliquidProduceMaintenanceMonth getlatest(String equipment_name, String maintenance_content) {
        return sjqSolidliquidProduceMaintenanceMonthMapper.getlatest(equipment_name, maintenance_content);
    }

    @Override
    public Integer[] is_lastweek_complete(String equipmentsName, String menstanceNum, String role) {
        String[] equipsName = equipmentsName.split("_");
        String[] menstNum = menstanceNum.split("_");
        Integer[] isLastweekComplete = new Integer[equipsName.length];
        for (int i = 0; i < equipsName.length; i++) {
            int num1, num2 = 0;
            num1 = (sjqSolidliquidProduceMaintenanceMonthMapper.getOperatorList(equipsName[i])).size();
            if (role.equals("examiner")){
                num2 = (sjqSolidliquidProduceMaintenanceMonthMapper.getExaminerList(equipsName[i])).size();
            }
            if (role.equals("operator")) {
                if (num1 == Integer.parseInt(menstNum[i]))
                    isLastweekComplete[i] = 2;
                else
                    isLastweekComplete[i] = 0;
            }
            else
                if (num1 == Integer.parseInt(menstNum[i]) && num2 == Integer.parseInt(menstNum[i])){
                    isLastweekComplete[i] = 2;
                }else if (num1 == Integer.parseInt(menstNum[i])){
                    isLastweekComplete[i] = 1;
                }else
                    isLastweekComplete[i] = 0;
        }

        return isLastweekComplete;
    }

    @Override
    public List<SjqSolidliquidProduceMaintenanceMonth> getMonthRecords(String equipmentName) {
        return sjqSolidliquidProduceMaintenanceMonthMapper.getMonthRecords(equipmentName);
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqSolidliquidProduceMaintenanceMonth> list = sjqSolidliquidProduceMaintenanceMonthMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqSolidliquidProduceMaintenanceMonth> list = sjqSolidliquidProduceMaintenanceMonthMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqSolidliquidProduceMaintenanceMonthMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqSolidliquidProduceMaintenanceMonth record) {
        return sjqSolidliquidProduceMaintenanceMonthMapper.updateByPrimaryKeySelective(record);
    }
}
